package cn.yi.newsys.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

/**
 * 数据库工具类
 * @author 祝愿辉
 *
 */
public class BaseDao {

	private Connection con = null ;
	private PreparedStatement ps = null ;
	private ResultSet rs = null ;
	private static String driver=null;
	private static String url=null;
	private static String user=null;
	private static String password=null; 
	
	/**
	 * 获取连接
	 * @return
	 */
	public Connection getConnection2(){
		try {
			// 通过 JNDI 查找 Tomcat 的数据源
			Context context = new InitialContext() ;
			DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/news") ;
			con = ds.getConnection() ;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return con ;
	}
	
	public Connection getConnection(){
		try {
			driver=ConfigManager.getInstance().getValue("driver");
			url=ConfigManager.getInstance().getValue("url");
			user=ConfigManager.getInstance().getValue("user");
			password=ConfigManager.getInstance().getValue("password");
			// 1.加载驱动(反射)
			Class.forName(driver);
			// 2.获取Connection对象
			con = DriverManager.getConnection(url, user, password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return con ;
	}
	
	
	/**
	 * 释放资源
	 */
	public void close(){
		/// 释放 ResultSet
		if( rs != null ){
			try {
				rs.close() ;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		// 释放 PreparedStatement
		if( ps != null){
			try {
				ps.close() ;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		// 释放 Connection
		if( con != null){
			try {
				con.close() ;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 增删改通用方法
	 * @param sql
	 * @param objs
	 * @return
	 */
	public int executeUpdate(String sql,Object[] objs){
		int row = 0 ;
		// 获取连接
		getConnection() ;
		try {
			ps = con.prepareStatement(sql);
			if(objs != null){
				// 设置参数
				for(int i = 0 ; i < objs.length ; i ++){
					ps.setObject(i+1, objs[i]) ;
				}
			}
			// 执行 SQL 语句
			row = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return row ;
	}
	
	/**
	 * 查询通用方法
	 * @param sql
	 * @param objs
	 * @return
	 */
	public ResultSet executeQuery(String sql,Object objs[]){
		// 获取连接
		getConnection() ;
		try {
			ps = con.prepareStatement(sql);
			if(objs != null){
				// 设置参数
				for(int i = 0 ; i < objs.length ; i ++){
					ps.setObject(i+1, objs[i]) ;
				}
			}
			// 执行 SQL 语句
			rs = ps.executeQuery() ;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rs ;
	}
	
}
